<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>await制作加载进度条</title>
    <style>
        div{
            height: 30px;
            background: greenyellow;
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
        }
    </style>
</head>
<body>
    <div id = "loading"></div>
    <script>
        function ajax(url){
            return new Promise((resolve,reject) => {
                if(!/^http/.test(url)){
                    throw new ParamError("请求地址格式错误");
                }

                let xhr = new XMLHttpRequest();
                xhr.open("GET",url);
                xhr.send();
                xhr.onload = function(){
                    if(this.status == 200){
                        resolve(JSON.parse(this.response));
                    }else if(this.status == 404){
                        reject(new HttpError("用户不存在"));
                    }else{
                        reject("加载失败");
                    }
                };
                xhr.onerror = function (){
                    reject(this);
                }
            })
        }

        function query(type){
            let url = "http://localhost:8008/index.php/api/home/getJiaDianList?type=";
            return ajax(`${url}${type}`);
        }

        async function loadJiaDian(types){
            for(let i = 0; i < types.length; i++){
                let type = await query(types[i]);
                let progress = ((i + 1) / types.length) * 100;
                loading.style.width = progress + "%";
                loading.innerHTML = Math.round(progress) + "%";
            }
        }

        loadJiaDian([1,2,3]);

    </script>
</body>
</html>